package com.tomorrow.runner.activity.basic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.tomorrow.runner.data.bean.UserBean;
import com.tomorrow.runner.service.api.IAccountService;
import com.tomorrow.runner.service.impl.ServiceManager;
import com.tomorrow.runner.util.AndroidBuild;
import com.tomorrow.runner.util.Define;

import java.util.HashMap;
import java.util.Map;

import cn.sucun.android.utils.Preferences;

/**
 * 基础界面类
 * Created by tomorrow on 16-4-20.
 */
public abstract class BasicActivity extends FragmentActivity implements SoundPool.OnLoadCompleteListener {
    protected IAccountService mAccountService;
    private UserBean userBean;
    private SoundPool mSoundPool;
    private final int MAX_SOUNDS = 5;
    protected Preferences mPreferences;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAccountService = (IAccountService) ServiceManager.getInstance().getService(ServiceManager.ACCOUNT_SERVICE);
        userBean = mAccountService.getUserInfo();
        registerReceiver(mLogoutReceiver,new IntentFilter(Define.ACTION_LOGOUT));
        mPreferences = Preferences.getCommon(this);
    }

    protected void showMsgToast(final String msg){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BasicActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    protected UserBean getUserInfo(){
        return userBean;
    }
    private BroadcastReceiver mLogoutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Define.ACTION_LOGOUT.equals(intent.getAction())){
                finish();
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLogoutReceiver);
    }

    private Map<Integer,Integer> mSoundsQueue;

    public void playSound(int id){
        if (mSoundPool == null){
            mSoundsQueue = new HashMap<>();
            if (AndroidBuild.isGreatThanOrEqualSdk(Build.VERSION_CODES.LOLLIPOP)){
                SoundPool.Builder builder = new SoundPool.Builder();
                builder.setMaxStreams(MAX_SOUNDS);
                builder.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build());
                mSoundPool = builder.build();
            }
            else{
                mSoundPool =  new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC, 0);
            }
            mSoundPool.setOnLoadCompleteListener(this);
        }
        mSoundsQueue.put(1,mSoundPool.load(this,id,1));
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        if (status == 0){
            soundPool.play(mSoundsQueue.get(1),1, 1, 0, 0, 1);
        }
    }
}
